#include<iostream>
#include <jsoncpp/json/json.h>

#include"common.hpp"
#include"compileAndRun.hpp" 
#include"../ThirdLibrary/httplib.h"


int main(int arg,char* argv[])
{
    if(2!=arg){
        printf("usage error: %s\nUsage: ./cr_server port\n",argv[0]);
        return -1;
    }
    /*
    上一层通过网络请求发过来的json串：
    "code" : "..."
    "input" : "..." 
    "timelimit" : "..."
    "spacelimit" : "..."

    我们返回给上层的json串：
    "status" : "..."
    "reason" : "..."
    "stdout" : "..."
    "stderr" : "..."
    */

    httplib::Server svr;
    // 要求URL请求的目录为 /compileAndRun，并在正文部分携带编译所需要的json串
    svr.Post("/compileAndRun", [](const httplib::Request &req, httplib::Response &resp){
        std::string in_json = req.body;
        std::string out_json;
        if(!in_json.empty()){
            ns_cr::CR::Start(in_json,out_json);
            resp.set_content(out_json, "application/json;charset=utf-8");
        }
    });

    svr.listen("0.0.0.0", atoi(argv[1])); //启动http服务

    return 0;
}